package com.wangxi.boot11demo.controller;

import com.wangxi.boot11demo.entity.User;
import com.wangxi.boot11demo.entity.Weibo;
import com.wangxi.boot11demo.mapper.WeiboMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
public class WeiboController {
    @Autowired(required = false)
    WeiboMapper mapper;

    @RequestMapping("/insert")
    public int insert(@RequestBody Weibo weibo, HttpSession session){
        User u = (User) session.getAttribute("user");
        if(u==null){
            return 2;
        }
        weibo.setCreated(new Date());
        weibo.setNick(u.getNick());
        weibo.setUserId(u.getId());
        System.out.println("weibo = " + weibo);
        mapper.insert(weibo);
        return 1;
    }
}
